package com.lyd.springtestapi.dao;

import com.lyd.springtestapi.domain.t_user;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 增删改查操作示例
 */
@Transactional//加上事物声明 @Transactional(readOnly = true)
public interface t_userRepository extends JpaRepository<t_user, Integer> {
    @Query(value = "SELECT * FROM t_user WHERE id=? and role=?;", nativeQuery = true)
    List<t_user> getAllUser(Integer id, Integer role);

    //    @Transactional//加上事物声明
    @Modifying(clearAutomatically = true)//自动清除实体里保存的数据(更新缓存)。
    @Query(value = "update t_user set username = ? where id = ?;", nativeQuery = true)
    int setUserName(String username, Integer id);


    @Modifying
    @Query(value = "delete from t_user  where id = ?", nativeQuery = true)
    int deleteUser(Integer id);

    @Modifying
    @Query(value = "INSERT INTO t_user VALUES (?, ?, ?, '123456' ,?, ?, ?, ?, ?);", nativeQuery = true)
    int insertUser(Integer id, Integer role, String username, String email, String gender, String createtime, Integer flag, String activatecode);
}
